package com.yuma.pandc;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.ThreadLocalRandom;

class NumberContainer2{
    private ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<>(3);

    public void push(int num){
        try{
            queue.put(num);
            System.out.println(Thread.currentThread().getName() + " push " + num);
        }catch(InterruptedException e){
            e.printStackTrace();
        }
    }

    public void pop(){
        try{
            Integer num = queue.take();
            System.out.println(Thread.currentThread().getName() + " pop " + num);
        }catch(InterruptedException e){
            e.printStackTrace();
        }
    }
}

public class ProdAndConsBlockingQueue {
    public static void main(String[] args) {
        NumberContainer2 container = new NumberContainer2();

        for(var i = 0; i < 5; i++){
            new Thread(() -> {
                container.push(ThreadLocalRandom.current().nextInt(10));
            }, "push"+i).start();
        }

        for(var i = 0; i < 5; i++){
            new Thread(() -> {
                container.pop();
            }, "pop"+i).start();
        }
    }
}
